import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";

/* eslint-disable prefer-destructuring */
import React, { PureComponent } from 'react';
import { Tabs, Row, Col, Input, List } from 'antd';
import Year from "./Content/Year";
import Month from "./Content/Month";
import Week from "./Content/Week";
import Day from "./Content/Day";
import Hour from "./Content/Hour";
import Minute from "./Content/Minute";
import Second from "./Content/Second";
var TabPane = Tabs.TabPane;

var Cron = /*#__PURE__*/function (_PureComponent) {
  _inherits(Cron, _PureComponent);

  var _super = _createSuper(Cron);

  function Cron(props) {
    var _this;

    _classCallCheck(this, Cron);

    _this = _super.call(this, props);

    _this.onChange = function (type, value) {
      _this.state[type].value = value;

      _this.setState(_objectSpread({}, _this.state), function () {
        _this.parse();

        _this.triggerChange();
      });
    };

    var date = new Date();
    _this.state = {
      activeKey: 'second',
      year: {
        type: '',
        start: date.getFullYear(),
        end: date.getFullYear()
      },
      month: {
        start: '',
        end: '',
        begin: '',
        beginEvery: '',
        type: '*',
        some: []
      },
      week: {
        start: '',
        end: '',
        last: '',
        begin: '',
        beginEvery: '',
        type: '?',
        some: []
      },
      day: {
        last: '',
        closeWorkDay: '',
        start: '',
        end: '',
        begin: '',
        beginEvery: '',
        type: '*',
        some: []
      },
      hour: {
        start: '',
        end: '',
        begin: '',
        beginEvery: '',
        type: '*',
        some: []
      },
      minute: {
        start: '',
        end: '',
        begin: '',
        beginEvery: '',
        type: '*',
        some: []
      },
      second: {
        start: '',
        end: '',
        begin: '',
        beginEvery: '',
        type: '*',
        some: []
      }
    };
    return _this;
  }

  _createClass(Cron, [{
    key: "initValue",
    value: function initValue() {
      var _this2 = this;

      var value = this.props.value ? this.props.value.toUpperCase() : '* * * * * *';
      var valuesArray = value.split(' ');

      var newState = _objectSpread({}, this.state);

      newState.second.value = valuesArray[0] || '';
      newState.minute.value = valuesArray[1] || '';
      newState.hour.value = valuesArray[2] || '';
      newState.day.value = valuesArray[3] || '';
      newState.month.value = valuesArray[4] || '';
      newState.week.value = valuesArray[5] || '';
      newState.year.value = valuesArray[6] || '';
      this.setState(newState, function () {
        _this2.parse();
      });
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount(props) {
      this.initValue(props);
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(props) {
      if (props.value !== this.props.value && this.props.value) {
        this.initValue();
      }
    }
  }, {
    key: "parse",
    value: function parse() {
      var _this$state = this.state,
          year = _this$state.year,
          month = _this$state.month,
          week = _this$state.week,
          day = _this$state.day,
          hour = _this$state.hour,
          minute = _this$state.minute,
          second = _this$state.second;

      if (year.value.indexOf('-') > -1) {
        year.type = 'period';
        var period = year.value.split('-');
        year.start = period[0];
        year.end = period[1];
      } else {
        year.type = year.value;
      }

      if (week.value.indexOf('-') > -1) {
        week.type = 'period';

        var _period = week.value.split('-');

        week.start = _period[0];
        week.end = _period[1];
      } else if (week.value.indexOf('L') > -1) {
        week.type = 'last';
        week.last = week.value.split('L')[0] || 1;
      } else if (week.value.indexOf('#') > -1) {
        week.type = 'beginInterval';
        week.begin = week.value.split('#')[0];
        week.beginEvery = week.value.split('#')[1];
      } else if (week.value.indexOf(',') > -1 || /^[0-9]+$/.test(week.value)) {
        week.type = 'some';
        week.some = week.value.split(',');
      } else {
        week.type = week.value || '?';
      }

      if (month.value.indexOf('-') > -1) {
        month.type = 'period';
        month.start = month.value.split('-')[0];
        month.end = month.value.split('-')[1];
      } else if (month.value.indexOf('/') > -1) {
        month.type = 'beginInterval';
        month.begin = month.value.split('/')[0];
        month.beginEvery = month.value.split('/')[1];
      } else if (month.value.indexOf(',') > -1 || /^[0-9]+$/.test(month.value)) {
        month.type = 'some';
        month.some = month.value.split(',');
      } else {
        month.type = month.value || '?';
      }

      if (day.value.indexOf('-') > -1) {
        day.type = 'period';
        day.start = day.value.split('-')[0];
        day.end = day.value.split('-')[1];
      } else if (day.value.indexOf('W') > -1) {
        day.type = 'closeWorkDay';
        day.closeWorkDay = day.value.split('W')[0];
      } else if (day.value.indexOf('L') > -1) {
        day.type = 'last';
        day.last = day.value.split('L')[0] || 1;
      } else if (day.value.indexOf('/') > -1) {
        day.type = 'beginInterval';
        day.begin = day.value.split('/')[0];
        day.beginEvery = day.value.split('/')[1];
      } else if (day.value.indexOf(',') > -1 || /^[0-9]+$/.test(day.value)) {
        day.type = 'some';
        day.some = day.value.split(',');
      } else {
        day.type = day.value || '?';
      }

      if (hour.value.indexOf('-') > -1) {
        hour.type = 'period';
        hour.start = hour.value.split('-')[0];
        hour.end = hour.value.split('-')[1];
      } else if (hour.value.indexOf('/') > -1) {
        hour.type = 'beginInterval';
        hour.begin = hour.value.split('/')[0];
        hour.beginEvery = hour.value.split('/')[1];
      } else if (hour.value.indexOf(',') > -1 || /^[0-9]+$/.test(hour.value)) {
        hour.type = 'some';
        hour.some = hour.value.split(',');
      } else {
        hour.type = hour.value || '?';
      }

      if (minute.value.indexOf('-') > -1) {
        minute.type = 'period';
        minute.start = minute.value.split('-')[0];
        minute.end = minute.value.split('-')[1];
      } else if (minute.value.indexOf('/') > -1) {
        minute.type = 'beginInterval';
        minute.begin = minute.value.split('/')[0];
        minute.beginEvery = minute.value.split('/')[1];
      } else if (minute.value.indexOf(',') > -1 || /^[0-9]+$/.test(minute.value)) {
        minute.type = 'some';
        minute.some = minute.value.split(',');
      } else {
        minute.type = minute.value || '?';
      }

      if (second.value.indexOf('-') > -1) {
        second.type = 'period';
        second.start = second.value.split('-')[0];
        second.end = second.value.split('-')[1];
      } else if (second.value.indexOf('/') > -1) {
        second.type = 'beginInterval';
        second.begin = second.value.split('/')[0];
        second.beginEvery = second.value.split('/')[1];
      } else if (second.value.indexOf(',') > -1 || /^[0-9]+$/.test(second.value)) {
        second.type = 'some';
        second.some = second.value.split(',');
      } else {
        second.type = second.value || '?';
      }

      this.setState({
        year: _objectSpread({}, year),
        month: _objectSpread({}, month),
        week: _objectSpread({}, week),
        day: _objectSpread({}, day),
        hour: _objectSpread({}, hour),
        minute: _objectSpread({}, minute),
        second: _objectSpread({}, second)
      });
    }
  }, {
    key: "format",
    value: function format() {
      var _this$state2 = this.state,
          year = _this$state2.year,
          month = _this$state2.month,
          week = _this$state2.week,
          day = _this$state2.day,
          hour = _this$state2.hour,
          minute = _this$state2.minute,
          second = _this$state2.second;
      return "".concat(second.value, " ").concat(minute.value, " ").concat(hour.value, " ").concat(day.value, " ").concat(month.value, " ").concat(week.value, " ").concat(year.value);
    }
  }, {
    key: "changeState",
    value: function changeState(state) {
      var _this3 = this;

      this.setState(state, function () {
        _this3.culcCron();
      });
    } // 计算用户的cron

  }, {
    key: "culcCron",
    value: function culcCron() {
      var _this4 = this;

      var n2s = this.n2s;
      var _this$state3 = this.state,
          year = _this$state3.year,
          month = _this$state3.month,
          week = _this$state3.week,
          day = _this$state3.day,
          hour = _this$state3.hour,
          minute = _this$state3.minute,
          second = _this$state3.second;

      if (year.type === 'period') {
        year.value = "".concat(n2s(year.start), "-").concat(n2s(year.end));
      } else {
        year.value = year.type;
      }

      if (month.type === 'period') {
        month.value = "".concat(n2s(month.start), "-").concat(n2s(month.end));
      } else if (month.type === 'beginInterval') {
        month.value = "".concat(n2s(month.begin), "/").concat(n2s(month.beginEvery));
      } else if (month.type === 'some') {
        month.value = month.some.join(',');
      } else {
        month.value = month.type;
      }

      if (week.type === 'period') {
        week.value = "".concat(n2s(week.start), "-").concat(n2s(week.end));
      } else if (week.type === 'beginInterval') {
        week.value = "".concat(n2s(week.begin), "#").concat(n2s(week.beginEvery));
      } else if (week.type === 'last') {
        week.value = "".concat(n2s(week.last), "L");
      } else if (week.type === 'some') {
        week.value = week.some.join(',');
      } else {
        week.value = week.type;
      }

      if (day.type === 'period') {
        day.value = "".concat(n2s(day.start), "-").concat(n2s(day.end));
      } else if (day.type === 'beginInterval') {
        day.value = "".concat(n2s(day.begin), "/").concat(n2s(day.beginEvery));
      } else if (day.type === 'closeWorkDay') {
        day.value = "".concat(n2s(day.closeWorkDay), "W");
      } else if (day.type === 'last') {
        day.value = "".concat(n2s(day.last), "L");
      } else if (day.type === 'some') {
        day.value = day.some.join(',');
      } else {
        day.value = day.type;
      }

      if (hour.type === 'period') {
        hour.value = "".concat(n2s(hour.start), "-").concat(n2s(hour.end));
      } else if (hour.type === 'beginInterval') {
        hour.value = "".concat(n2s(hour.begin), "/").concat(n2s(hour.beginEvery));
      } else if (hour.type === 'some') {
        hour.value = hour.some.join(',');
      } else {
        hour.value = hour.type;
      }

      if (minute.type === 'period') {
        minute.value = "".concat(n2s(minute.start), "-").concat(n2s(minute.end));
      } else if (minute.type === 'beginInterval') {
        minute.value = "".concat(n2s(minute.begin), "/").concat(n2s(minute.beginEvery));
      } else if (minute.type === 'some') {
        minute.value = minute.some.join(',');
      } else {
        minute.value = minute.type;
      }

      if (second.type === 'period') {
        second.value = "".concat(n2s(second.start), "-").concat(n2s(second.end));
      } else if (second.type === 'beginInterval') {
        second.value = "".concat(n2s(second.begin), "/").concat(n2s(second.beginEvery));
      } else if (second.type === 'some') {
        second.value = second.some.join(',');
      } else {
        second.value = second.type;
      }

      this.setState({
        year: _objectSpread({}, year),
        month: _objectSpread({}, month),
        week: _objectSpread({}, week),
        day: _objectSpread({}, day),
        hour: _objectSpread({}, hour),
        minute: _objectSpread({}, minute),
        second: _objectSpread({}, second)
      }, function () {
        _this4.triggerChange();
      });
    }
  }, {
    key: "n2s",
    value: function n2s(number) {
      // eslint-disable-next-line use-isnan
      if (typeof number === 'number' && number !== NaN) {
        return "".concat(number);
      }

      return number;
    }
  }, {
    key: "triggerChange",
    value: function triggerChange() {
      // eslint-disable-next-line no-unused-expressions
      this.props.onChange && this.props.onChange(this.format());
    } // 发生表单值改变，重新计算

  }, {
    key: "renderOverLay",
    value: function renderOverLay() {
      var _this5 = this;

      var activeKey = this.state.activeKey;
      return /*#__PURE__*/React.createElement(Tabs, {
        activeKey: activeKey,
        onChange: function onChange(key) {
          _this5.setState({
            activeKey: key
          });
        }
      }, /*#__PURE__*/React.createElement(TabPane, {
        tab: "\u79D2",
        key: "second"
      }, /*#__PURE__*/React.createElement(Second, Object.assign({}, this.state, {
        onChange: function onChange(state) {
          _this5.changeState({
            second: state
          });
        }
      }))), /*#__PURE__*/React.createElement(TabPane, {
        tab: "\u5206\u949F",
        key: "minute"
      }, /*#__PURE__*/React.createElement(Minute, Object.assign({}, this.state, {
        onChange: function onChange(state) {
          _this5.changeState({
            minute: state
          });
        }
      }))), /*#__PURE__*/React.createElement(TabPane, {
        tab: "\u5C0F\u65F6",
        key: "hour"
      }, /*#__PURE__*/React.createElement(Hour, Object.assign({}, this.state, {
        onChange: function onChange(state) {
          _this5.changeState({
            hour: state
          });
        }
      }))), /*#__PURE__*/React.createElement(TabPane, {
        tab: "\u65E5",
        key: "day"
      }, /*#__PURE__*/React.createElement(Day, Object.assign({}, this.state, {
        onChange: function onChange(state) {
          _this5.changeState({
            day: state
          });
        }
      }))), /*#__PURE__*/React.createElement(TabPane, {
        tab: "\u6708",
        key: "month"
      }, /*#__PURE__*/React.createElement(Month, Object.assign({}, this.state, {
        onChange: function onChange(state) {
          _this5.changeState({
            month: state
          });
        }
      }))), /*#__PURE__*/React.createElement(TabPane, {
        tab: "\u5468",
        key: "week"
      }, /*#__PURE__*/React.createElement(Week, Object.assign({}, this.state, {
        onChange: function onChange(state) {
          _this5.changeState({
            week: state
          });
        }
      }))), /*#__PURE__*/React.createElement(TabPane, {
        tab: "\u5E74",
        key: "year"
      }, /*#__PURE__*/React.createElement(Year, Object.assign({}, this.state, {
        onChange: function onChange(state) {
          _this5.changeState({
            year: state
          });
        }
      }))));
    }
  }, {
    key: "render",
    value: function render() {
      var _this6 = this;

      var state = JSON.parse(JSON.stringify(this.state));
      var year = state.year,
          month = state.month,
          week = state.week,
          day = state.day,
          hour = state.hour,
          minute = state.minute,
          second = state.second;
      return /*#__PURE__*/React.createElement("div", {
        className: "antd-cron"
      }, this.renderOverLay(), /*#__PURE__*/React.createElement(List, {
        bordered: true,
        style: {
          marginTop: 10
        }
      }, /*#__PURE__*/React.createElement(List.Item, null, /*#__PURE__*/React.createElement(Row, {
        type: "flex",
        gutter: 5,
        style: {
          width: '100%',
          textAlign: 'center'
        }
      }, /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, "\u79D2"), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, "\u5206"), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, "\u5C0F\u65F6"), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, "\u5929"), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, "\u6708"), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, "\u661F\u671F"), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, "\u5E74"))), /*#__PURE__*/React.createElement(List.Item, null, /*#__PURE__*/React.createElement(Row, {
        type: "flex",
        gutter: 5,
        style: {
          width: '100%',
          textAlign: 'center'
        }
      }, /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, /*#__PURE__*/React.createElement(Input, {
        value: second.value,
        onChange: function onChange(e) {
          _this6.onChange('second', e.target.value);
        }
      })), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, /*#__PURE__*/React.createElement(Input, {
        value: minute.value,
        onChange: function onChange(e) {
          _this6.onChange('minute', e.target.value);
        }
      })), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, /*#__PURE__*/React.createElement(Input, {
        value: hour.value,
        onChange: function onChange(e) {
          _this6.onChange('hour', e.target.value);
        }
      })), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, /*#__PURE__*/React.createElement(Input, {
        value: day.value,
        onChange: function onChange(e) {
          _this6.onChange('day', e.target.value);
        }
      })), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, /*#__PURE__*/React.createElement(Input, {
        value: month.value,
        onChange: function onChange(e) {
          _this6.onChange('month', e.target.value);
        }
      })), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, /*#__PURE__*/React.createElement(Input, {
        value: week.value,
        onChange: function onChange(e) {
          _this6.onChange('week', e.target.value);
        }
      })), /*#__PURE__*/React.createElement(Col, {
        span: 3
      }, /*#__PURE__*/React.createElement(Input, {
        value: year.value,
        onChange: function onChange(e) {
          _this6.onChange('year', e.target.value);
        }
      }))))));
    }
  }]);

  return Cron;
}(PureComponent);

export default Cron;